<?php

namespace app\model;

use app\stateData;
use think\Model;

class SystemDynamicFrom extends Model
{

    /**
     * 动态表单数据表
     */

    protected $createTime = 'create_date';

    /**
     * 关联system_menu表
     * alias_id外键
     * @illustrate alias_id 为批量字符串，暂弃用关联模板
     */
    public function profile(): \think\model\relation\HasOne
    {
        return $this->hasOne('system_menu','alias_id','alias_id');
    }

    /**
     * 获取动态表单模板列表
     * @param $page
     * @return SystemDynamicFrom[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getFromList($page,$pageNum,$where)
    {
//        return SystemDynamicFrom::withjoin(['profile'=>['menu_name']])->page($page,stateData::$_page)->select();

        return $this->where('template_name','like','%'.$where.'%')->page($page,$pageNum)->order('id ASC')->select();
    }
    public function getFromCount()
    {
//        return SystemDynamicFrom::withjoin(['profile'=>['menu_name']])->page($page,stateData::$_page)->select();

        return $this->count();
    }

    /**
     * 添加动态模板
     * @param $data
     * @return SystemDynamicFrom|Model
     */
    public function addFromModel($data)
    {
        return self::create($data);
    }

    /**
     * 修改动态模板
     * @param $data
     * @return SystemDynamicFrom
     */
    public function editFromModel($data): SystemDynamicFrom
    {
        return self::update($data);
    }

    /**
     * 删除动态模板
     * @param $id
     * @return bool
     */
    public function delFromModel($id)
    {
        return self::destroy($id);
    }

    /**
     * 通过别名id查询对应模板id
     * @param $alias
     * @return mixed
     */
    public function getFromId($alias)
    {
        return $this->where(['alias_id'=>$alias])->value ('id');
    }

    /**
     * 通过菜单id找模板id
     * @param $menuId
     * @return mixed
     */
    public function getMenuAliasIdBindFieldId($menuId)
    {
        return $this->where (['menu_id'=>$menuId])->value('id');
    }

    /**
     * 查询别名是否已经存在
     * @param $alias
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getAliasIdEx($alias):bool
    {
        return (bool)$this->where(['alias_id'=>$alias])->find ();
    }


}